﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;



namespace UIKit
{
    public class UIView : UIResponder
    {
        #region properties
        public float alpha = 1.0f;
        public bool autoresizesSubviews = true;
        public UIViewAutoresizing autoresizingMask;
        public Color backgroundColor;
        public Rect bounds;

        public Point center;
        public bool clearsContextBeforeDrawing;
        public bool clipsToBounds;
        public UIViewContentMode contentMode;
        public Rect contentStretch;
        public bool exclusiveTouch;
        public Rect frame;
        public bool hidden
        {
            get
            {
                if (viewElement != null)
                {
                    return (
                        this.viewElement.Visibility == Visibility.Collapsed
                        );
                }
                else
                {
                    return false;
                }
            }
            set
            {
                this.viewElement.Visibility = (value == true) ? Visibility.Collapsed : Visibility.Visible;
            }
        }

        /// <summary>
        /// The view’s Core Animation layer used for rendering. (read-only)
        /// This will not be needed in WPF implementation
        /// </summary>
        public readonly UIElement layer;

        public bool multipleTouchEnabled;
        public bool opaque = true;
        public List<UIView> subviews;
        public UIView superview;
        public int tag;

        // LG: Was a CGAffineTransform
        public TransformGroup transform;
        public bool userInteractionEnabled = true;
        public UIWindow window;

        #endregion

        #region instance methods

        public void addSubview(UIView view)
        {
            try
            {
                Panel parentPanel = null;
                if (this.viewElement is Panel) {
                    parentPanel = viewElement as Panel;
                }
                else if (viewElement is ContentControl)
                {
                    ContentControl ctrl = viewElement as ContentControl;
                    if (ctrl.Content == null)
                    {
                        ctrl.Content = (parentPanel = new Grid());
                    }
                    else {
                        if (ctrl.Content is Panel)
                        {
                            parentPanel = ctrl.Content as Panel;
                        }
                    }
                }

                if (parentPanel != null)
                {
                    parentPanel.Children.Add(view.viewElement);
                    parentPanel.UpdateLayout();
                    // LG: This should be done through observing the children?
                    subviews.Add(view); 
                }
                else
                {
                    throw new Exception("Could not load the view: UIView.addSubiew");
                }
            }
            catch
            {
                return;
            }
            didAddSubview(view);
        }


        public void bringSubviewToFront(UIView view) { }
        public Point convertPointFromView(Point point, UIView view)
        {
            return new Point(0, 0);
        }
        public Point convertPointToView(Point point, UIView view)
        {
            return new Point(0, 0);
        }
        public Rect convertRectFromView(Rect rect, UIView view)
        {
            return new Rect(0, 0, 0, 0);
        }
        public Rect convertRectToView(Rect rect, UIView view)
        {
            return new Rect(0, 0, 0, 0);
        }

        public virtual void didAddSubview(UIView subview) { }
        public virtual void didMoveToSuperview() { }
        public virtual void didMoveToWindow() { }
        public virtual void drawRect(Rect rect)
        {

        }

        public void endEditing(bool force)
        {

        }

        public void exchangeSubviewAtIndex(int index1, int index2) { }
        public void hitTest(Point point, UIEvent e) { }

        public void initWithFrame(Rect aRect)
        {
            if (this.viewElement != null)
            {
                this.viewElement.Width = aRect.Width;
                this.viewElement.Height = aRect.Height;
                // we can't change top/left as the parent element in WPF controls this
                // e.g. Canvas.Top = "100"
                // Canvas.SetTop(this.viewElement, 100);
            }
        }
        public void insertSubviewAboveSubview(UIView view, UIView siblingSubview) { }
        public void insertSubviewAtIndex(UIView view, int index) { }
        public void insertSubviewBelowSubview(UIView view, UIView siblingSubview) { }
        public bool isDescendantOfView(UIView view) { return true; }

        public void layoutIfNeeded()
        {
            // if (isNeeded) {}
        }

        public virtual void layoutSubviews() { }
        public bool pointInside(Point point, UIEvent e)
        {
            return true;
        }

        public void removeFromSuperview() { }
        public void sendSubviewToBack(UIView view) { }
        public void setNeedsDisplay()
        {

        }

        public void setNeedsDisplayInRect(Rect invalidRect)
        {

        }

        /// <summary>
        /// Not Used: this is purely a window drawing method, wpf should not require this.
        /// </summary>
        public void setNeedsLayout() { }

        public Size sizeThatFits(Size size)
        {
            return new Size(0, 0);
        }

        public void sizeToFit() { }
        public UIView viewWithTag(int tag) { return new UIView(); }
        public void willMoveToSuperview(UIView newSuperview) { }
        public void willMoveToWindow(UIWindow newWindow) { }
        public void willRemoveSubview(UIView subview) { }
        #endregion

        #region static (class) methods
        public static bool areAnimationsEnabled() { return true; }

        /// <summary>
        /// May be used: Animations will be controlled through WPF Animations.
        /// Context might be the DataContext of this view?
        /// </summary>
        /// <param name="animationID"></param>
        /// <param name="context"></param>
        public static void beginAnimations(string animationID, object context) { }
        public static void commitAnimations() { }

        /// <summary>
        /// Replaced "Class" with "Type"
        /// </summary>
        /// <returns></returns>
        public static Type layerClass() { return typeof(UIView); }
        public static void setAnimationBeginsFromCurrentState(bool fromCurrentState) { }
        public static void setAnimationCurve(UIViewAnimationCurve curve) { }
        public static void setAnimationDelay(double delay) { }
        public static void setAnimationDelegate(object theDelegate) { }
        public static void setAnimationDidStopSelector(AnimationDidStopSelector selector) { }
        public static void setAnimationDuration(double duration) { }
        public static void setAnimationRepeatAutoreverses(bool repeatAutoreverses) { }
        public static void setAnimationRepeatCount(float repeatCount) { }
        public static void setAnimationsEnabled(bool enabled) { }
        public static void setAnimationStartDate(DateTime startTime) { }
        public static void setAnimationTransition(UIViewAnimationTransition transition, UIView view, bool cache) { }
        public static void setAnimationWillStartSelector(AnimationWillStartSelector selector) { }
        #endregion

        #region delegates (not IUKit)
        public delegate void AnimationDidStopSelector(string animationID, bool finished, object context);
        public delegate void AnimationWillStartSelector(string animationID, object context);
        #endregion

        #region non-UIKit code

        #region properties
        /// <summary>
        /// used to store the WPF visual element associated with this view.
        /// </summary>
        public FrameworkElement viewElement;
        #endregion

        #region methods
        
        // LG: Nothing yet
        
        #endregion

        #endregion
    }



}
